//
//  RootViewController.swift
//  LEOweibo
//
//  Created by zz on 16/2/22.
//  Copyright © 2016年 zz. All rights reserved.
//

import UIKit
import HMRefresh
class RootViewController: UIViewController {
    //MARK:- 公共属性
   var tableView: UITableView?
    
    lazy var pullUpView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
    
    var refreshControl: HMRefreshControl?
    
    var visitorView :VistorView?
    //MARK:- 视图生命周期
    override func loadView() {
        
        UserAccount.sharedUserAccount.isLogin ? setUpTableView() : setUpVistorView()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        if UserAccount.sharedUserAccount.isLogin{
            
            loadData()
        }else{
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "loginSuccess", name: LeoLoginSuccessedNotification, object: nil)
            
        }
        //        refreshControl.beginRefreshing()
        // Do any additional setup after loading the view.
    }
    ///加载数据
    func loadData() {
        print("loadData")
        
        
    }
    ///收到内存警告方法
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
//MARK:- 监听方法
extension RootViewController{
    
    ///登录成功监听方法
    @objc func loginSuccess() {
        view = nil
        NSNotificationCenter.defaultCenter().removeObserver(self, name:LeoLoginSuccessedNotification, object: nil)
    }
    ///跳转授权控制器
    @objc private func didClickLoginButton (){
        let oauthVC = OAuthViewController()
        let nav = UINavigationController(rootViewController: oauthVC)
        presentViewController(nav, animated: true, completion: nil)
    }
}

//MARK:- 设置界面
extension RootViewController {
    
    ///设置访客视图
    func setUpVistorView() {
        visitorView = VistorView()
        view = visitorView
        NSNotificationCenter.defaultCenter().addObserver(self, selector:"didClickLoginButton" ,
            name:LeoDidClickLoginButton,
            object: nil)
    }
    ///设置表格视图
    func setUpTableView() {
        tableView = UITableView(frame: CGRectZero, style: .Plain)
        view = tableView
        tableView?.dataSource = self
        tableView?.delegate = self
    }
    
    ///设置下拉刷新控件
    func setUpRefreshControl(){
        guard let tableView = tableView else{
            return
        }
//        tableView.addSubview(refreshControl)
//        refreshControl.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
//        tableView.tableFooterView = pullUpView
        if refreshControl != nil {
            return
        }
        refreshControl = HMRefreshControl()
        tableView.addSubview(refreshControl!)
        
        // 设置下拉刷新视图
        refreshControl?.pulldownView = CZPulldownRefreshView.refreshView()
        refreshControl?.normalString = "下拉起飞"
        refreshControl?.pullingString = "放开起飞"
        refreshControl?.refreshingString = "正在起飞"
        refreshControl?.lastRefreshString = "末次 "
        refreshControl?.donotPullupString = "没有更多数据"
        
        // 设置刷新控件的监听方法
        refreshControl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)
    }
    
}

//MARK:- UITableViewDataSource,UITableViewDelegate
extension RootViewController:UITableViewDataSource,UITableViewDelegate {
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
//    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    
//        let section = tableView.numberOfSections - 1
//        let row = tableView.numberOfRowsInSection(section) - 1
        
//        if section == indexPath.section && row == indexPath.row && !pullUpView.isAnimating(){
//            
//            pullUpView.startAnimating()
//            loadData()
//        }
        
//    }
    
}







